package jsyf.oa.service.report;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.DateUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.email.EmailReceiverDao;
import jsyf.oa.facade.report.EmailReportFacade;
import jsyf.oa.facade.system.UserFacade;
import jsyf.oa.service.report.reportCommon.ReportCommonMethodsAndConstant;
import jsyf.oa.service.report.visitCustomer.VisitCustomerService;
import jsyf.oa.service.system.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Administrator on 2018/1/9 0009.
 */
@Service
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class EmailReportService implements EmailReportFacade {

    @Autowired
    private EmailReceiverDao emailReceiverDao;

    @Autowired
    private UserFacade userFacade;

    @Autowired
    private ReportCommonMethodsAndConstant common;

    @Override
    public List<JSONObject> emailReport(String reportDate, String readerName) {
        StringBuilder sbCondition = new StringBuilder();
        List<Object> sqlArgs = new ArrayList<Object>();
        if (!StringUtils.isBlank(reportDate)) {
            sbCondition.append(" AND eType = 1 AND reportDate = ? ");
            sqlArgs.add(reportDate);
        }
        if (!StringUtils.isBlank(readerName)) {
            sbCondition.append("  AND (readerName LIKE " + "'%" + readerName + "%'" + " "
                    + " or readerCompanyName LIKE " + "'%" + readerName + "%'" +  " " +
                    " or readerDepartName LIKE " + "'%" + readerName + "%'" +  " " +
                    " or readerRoleName LIKE " + "'%" + readerName + "%'" +  ")");
        }
        return emailReceiverDao.emailReport(sbCondition.toString(), sqlArgs.toArray());
    }

    @Override
    public List<JSONObject> emailReportForDay(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        //获取今日的日期
        Calendar c = Calendar.getInstance();
        String todayTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        todayTime=common.getYesterDayForTelTarget(todayTime);
        StringBuilder sbCondition = new StringBuilder();
        List<Object> sqlArgs = new ArrayList<Object>();
        List<JSONObject> jsonObjectList=userFacade.getVisiblePerson(jsonData);
        String ids="";
        for (JSONObject s:jsonObjectList) {
            ids+="'"+s.getString("id")+"',";
        }
        if (ids.endsWith(",")) {
            ids = ids.substring(0, ids.length() - 1);
        }
        if (!StringUtils.isBlank(todayTime)) {
            sbCondition.append(" AND eType = 1 AND reportDate = ? AND readerId = ? AND  senderId in ("+ids+")");
            sqlArgs.add(todayTime);
            sqlArgs.add(userId);
        }
        return emailReceiverDao.emailReportForDay(sbCondition.toString(), sqlArgs.toArray());
    }

}
